package cn.haixiang.org.controller;

import cn.haixiang.basice.util.AjaxResult;
import cn.haixiang.basice.util.PageList;
import cn.haixiang.org.domain.Employee;
import cn.haixiang.org.query.EmployeeQuery;
import cn.haixiang.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //这个是json格式和controller的结合体
@RequestMapping("/emp")
public class EmployeeController {

    //写增删改查的方法
    @Autowired
    private IEmployeeService employeeService;


    //查询单个信息
    //使用这个@PathVariable注解可以获取到接口路径中使用{}的占位符参数
//    @GetMapping("/{id}")
//    public Employee findEmployeeById(@PathVariable("id")Long id){
//        return employeeService.findById(id);
//    }


//
//    //删除接口
//    @DeleteMapping("/{id}")
//    public AjaxResult delEmployeeById(@PathVariable("id") Long id){//写@PathVariable是为了统一接口路径
//        try {
//            employeeService.delete(id);
//            return AjaxResult.getAjaxResult();
//        }catch (Exception e){
//            e.printStackTrace();
//            return AjaxResult.getAjaxResult().setSuccess(false).setMsg("系统饭吗嫩");
//        }
//    }
//
//    //添加或者修改
//    @PutMapping()
//    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
//        if (null != employee.getId()) {
//            // 修改
//            employeeService.upDate(employee);
//        } else {
//            // 添加
//            employeeService.add(employee);
//        }
//        return AjaxResult.me().success();
//    }


    //查询所有
    @GetMapping
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 添加或者修改
     * @param
     * @return
     */

    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        System.out.println(employee+"+++++++++++++++++++++++++++++++++++++++++++++");
        if (null!= employee.getId()){
            //进行修改
            employeeService.update(employee);
        }else {
            //进行新增
            employeeService.add(employee);
        }
        return AjaxResult.me().success();
    }
    //批量删除
    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        ids.forEach(System.out::println);
        return AjaxResult.me().success();
    }
    /**
     * 删除
     * @param
     * @return
     */
    //删除接口
    @DeleteMapping("/{id}")
    public AjaxResult delEmployeeById(@PathVariable("id") Long id) {//写@PathVariable是为了统一接口路径
        employeeService.delete(id);
        return AjaxResult.me().success();
    }


    //分页查询
    @PostMapping()
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query) {
        return employeeService.queryByPage(query);
    }


}
